<template>
  <el-card class="content">
    <el-row>
      <el-col :span="4">
        <span style="fontsize: 16px">测点名称:</span>
        <el-input
          size="mini"
          style="width: 200px"
          placeholder="请输入测点名称"
          v-model="query.monitorName"
          clearable
        >
        </el-input>
      </el-col>
      <el-col :span="4">
        <span style="fontsize: 16px">测点编号:</span>
        <el-input
          size="mini"
          style="width: 200px"
          placeholder="请输入测点编号"
          v-model="query.monitorCode"
          clearable
        >
        </el-input>
      </el-col>
      <el-col :span="4">
        <span style="fontsize: 16px">监测类型:</span>
        <el-select
          v-model="query.monitorTypeId"
          placeholder="请选择监测类型"
          size="mini"
          clearable
        >
          <el-option
            v-for="item in stationType"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="6">
        <el-button class="black-button" @click="searchList" size="small"
          >查询</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click="dialogVisible = true"
          >新增</el-button
        >
      </el-col>
      <el-col :span="6"> </el-col>
    </el-row>
    <p class="bg-purple">测点信息管理</p>
    <el-table stripe size="small" :data="stationList">
      <el-table-column
        prop="name"
        label="测点名称"
        align="center"
        width="80px"
      ></el-table-column>
      <el-table-column
        prop="monitorCode"
        label="测点编号"
        align="center"
        width="200px"
      ></el-table-column>
      <el-table-column
        prop="sensorName"
        label="设备名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="monitorTypeName"
        label="监测类型"
        align="center"
        width="80px"
      ></el-table-column>
      <el-table-column
        prop="tailingPondName"
        label="尾矿库名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="id"
        label="id"
        align="center"
        v-if="none"
      ></el-table-column>
      <el-table-column
        prop="longitude"
        label="经度"
        align="center"
        width="150px"
      ></el-table-column>
      <el-table-column
        prop="latitude"
        label="纬度"
        align="center"
        width="150px"
      ></el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="updateTime"
        label="更新时间"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" align="center" width="120px" prop="id">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="isEditDialogVisible(scope.row)"
            >修改</el-button
          >
          <el-button type="text" size="small" @click="delStation(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-row type="flex" justify="end">
      <!-- <el-col :span="10"> -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.page"
        :page-sizes="[10, 15]"
        :page-size="query.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- </el-col> -->
    </el-row>
    <!-- 添加弹窗 -->
    <el-dialog
      title="添加测点信息列表"
      :visible.sync="dialogVisible"
      :width="dialogWidth"
    >
      <el-form
        :model="stationForm"
        ref="stationForm"
        label-width="120px"
        size="mini"
        :rules="rules"
      >
        <el-form-item label="测点名称" size="mini" prop="name">
          <el-input
            v-model="stationForm.name"
            style="width: 535px"
            placeholder="请填写测点名称"
          ></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="测点编号" size="mini" prop="monitorCode">
              <el-input
                v-model="stationForm.monitorCode"
                style="width: 180px"
                placeholder="请填写测点编号"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备" size="mini" prop="sensorId">
              <el-select v-model="stationForm.sensorId" size="mini">
                <el-option
                  v-for="item in sensorCodelists"
                  :key="item.id"
                  :label="item.sensorName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="经度" size="mini" prop="longitude">
              <el-input
                v-model="stationForm.longitude"
                label-width="180px"
                placeholder="请填写经度"
                style="width: 180px"
              >
              </el-input> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="纬度" size="mini" prop="latitude">
              <el-input
                v-model="stationForm.latitude"
                style="width: 180px"
                placeholder="请填写纬度"
              >
              </el-input> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"
            ><el-form-item
              label="监测类型"
              label-width="120px"
              prop="monitorTypeId"
            >
              <el-select v-model="stationForm.monitorTypeId" size="mini">
                <el-option
                  v-for="item in stationType"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="尾矿库" size="mini" prop="tailingPondId">
              <el-select v-model="stationForm.tailingPondId" size="mini">
                <el-option
                  v-for="item in tailingList"
                  :key="item.id"
                  :label="item.tailingName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item class="buttonfrom">
          <el-button type="primary" @click="isdialogVisible">确 定</el-button>
          <el-button @click="closeDialogVisible">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 修改弹窗 -->
    <el-dialog
      title="修改测点信息"
      :visible.sync="editDialogVisible"
      :width="dialogWidth"
    >
      <el-form
        :model="editStationForm"
        ref="editStationForm"
        label-width="120px"
        size="mini"
        :rules="rules"
      >
        <el-form-item label="测点名称" size="mini" prop="name">
          <el-input
            v-model="editStationForm.name"
            style="width: 535px"
            placeholder="请填写测点名称"
          ></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="测点编号" size="mini" prop="monitorCode">
              <el-input
                v-model="editStationForm.monitorCode"
                style="width: 180px"
                placeholder="请填写测点编号"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备" size="mini" prop="sensorId">
              <el-select v-model="editStationForm.sensorId" size="mini">
                <el-option
                  v-for="item in sensorCodelists"
                  :key="item.id"
                  :label="item.sensorName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="经度" size="mini" prop=" longitude">
              <el-input
                v-model="editStationForm.longitude"
                label-width="180px"
                placeholder="请填写经度"
                style="width: 180px"
              >
              </el-input> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="纬度" size="mini" prop="latitude">
              <el-input
                v-model="editStationForm.latitude"
                style="width: 180px"
                placeholder="请填写纬度"
              >
              </el-input> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"
            ><el-form-item
              label="监测类型"
              label-width="120px"
              prop="monitorTypeId"
            >
              <el-select v-model="editStationForm.monitorTypeId" size="mini">
                <el-option
                  v-for="item in stationType"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="尾矿库名称" size="mini" prop="tailingPondId">
              <el-select v-model="editStationForm.tailingPondId" size="mini">
                <el-option
                  v-for="item in tailingList"
                  :key="item.id"
                  :label="item.tailingName"
                  :value="item.id"
                ></el-option> </el-select
            ></el-form-item>
          </el-col>
        </el-row>
        <el-form-item class="buttonfrom">
          <el-button type="primary" @click="isEditdialogVisible"
            >确 定</el-button
          >
          <el-button @click="closeEditDialogVisible">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </el-card>
</template>

<script>
import axios from "axios";
import {
  getStationInforPage,
  getTailingPondList,
  getSensorInforLists,
  getMonitorTypeList,
} from "../../../api/api";
import Axios from "axios";
export default {
  name: "StationInformationChild",
  data() {
    return {
      none: false,
      query: {
        page: 1,
        pagesize: 10,
        name: "",
        monitorCode: "",
      },
      stationList: [],
      total: null,
      dialogVisible: false,
      stationForm: {
        latitude: null,
        longitude: null,
        monitorTypeId: null,
        sensorId: null,
        tailingPondId: null,
        unitId: sessionStorage.getItem("unitId"),
        name: "",
        monitorCode: "",
      },
      none: false,
      dialogWidth: "750px",
      editStationForm: {
        latitude: null,
        longitude: null,
        monitorTypeId: null,
        sensorId: null,
        tailingPondId: null,
        unitId: sessionStorage.getItem("unitId"),
        id: null,
        name: "",
        monitorCode: "",
      },
      //测点类型
      stationType: [],
      sensorCodelists: [],
      rules: {
        latitude: [{ required: true, message: "请填写纬度", trigger: "blur" }],
        monitorCode: [
          { required: true, message: "请填写测点编号", trigger: "blur" },
        ],

        longitude: [{ required: true, message: "请填写经度", trigger: "blur" }],
        monitorTypeId: [
          { required: true, message: "请选择监测类型", trigger: "change" },
        ],
        name: [{ required: true, message: "请填写测点名称", trigger: "blur" }],
        sensorId: [
          { required: true, message: "请填写设备编号", trigger: "change" },
        ],
        tailingPondId: [
          { required: true, message: "请填写尾矿库名称", trigger: "change" },
        ],
      },
      editDialogVisible: false,
      tailingList: [],
    };
  },
  created() {
    this.getStaionListPage();
    this.getTypeList();
    this.getTailingList();
    this.sensorCodeList();
  },
  methods: {
    //分页查询列表
    async getStaionListPage() {
      const res = await getStationInforPage({
        pageCount: this.query.page,
        pageSize: this.query.pagesize,
        unitId: sessionStorage.getItem("unitId"),
        monitorName: this.query.monitorName,
        monitorCode: this.query.monitorCode,
        monitorTypeId: this.query.monitorTypeId,
      });
      if (res.resultCode == "200") {
        console.log(res);
        this.stationList = res.parameter.list;
        this.total = res.parameter.total;
      } else {
        this.stationList = [];
      }
    },
    // 查询按钮
    async searchList() {
      const res = await getStationInforPage({
        pageCount: this.query.page,
        pageSize: this.query.pagesize,
        unitId: sessionStorage.getItem("unitId"),
        monitorName: this.query.monitorName,
        monitorCode: this.query.monitorCode,
        monitorTypeId: this.query.monitorTypeId,
      });
      if (res.resultCode == "200") {
        this.$message({
          type: "success",
          message: "成功查询到数据",
          offset: 400,
          duration: 1000,
        });
        this.stationList = res.parameter.list;
      }
      if (res.resultCode == "204") {
        this.$message({
          message: "暂无数据",
          offset: 400,
          duration: 1000,
        });
        this.stationList = [];
      }
      this.total = res.parameter.total;
    },
    handleSizeChange(val) {
      this.query.pagesize = val;
      this.getStaionListPage();
    },
    handleCurrentChange(val) {
      this.query.page = val;
      this.getStaionListPage();
    },
    //查询测点类型
    async getTypeList() {
      const res = await getMonitorTypeList();
      console.log(res);
      this.stationType = res.parameter;
    },
    //查询尾矿库列表
    async getTailingList() {
      const res = await getTailingPondList({
        unitId: sessionStorage.getItem("unitId"),
      });
      console.log(res);
      this.tailingList = res.parameter;
    },
    // 查询设备编号
    async sensorCodeList() {
      const res = await getSensorInforLists();
      this.sensorCodelists = res.parameter;
    },
    // 添加确定
    async isdialogVisible() {
      this.$refs.stationForm.validate(async (val) => {
        if (val) {
          const formData = new FormData();
          for (const [key, value] of Object.entries(this.stationForm)) {
            formData.append(key, value);
          }
          axios({
            method: "post",
            url: "/monitorPoint/addMonitorPoint",
            data: formData,
          }).then((res) => {
            if (res.data.resultCode == "200") {
              this.getStaionListPage();
              this.$message({
                type: "success",
                message: "添加成功",
                offset: 400,
                duration: 1000,
              });
              this.$refs.stationForm.resetFields();
            } else if (res.data.resultCode == "400") {
              this.$message({
                type: "error",
                message: "添加失败",
                offset: 400,
                duration: 1000,
              });
            }
          });
          this.dialogVisible = false;
        } else {
          this.$message({
            type: "error",
            message: "请完善表单信息",
            offset: 400,
            duration: 1000,
          });
        }
      });
      this.$refs.stationForm.resetFields();
    },
    //添加取消
    closeDialogVisible() {
      this.dialogVisible = false;
      this.$refs.stationForm.resetFields();
    },
    //修改弹窗
    isEditDialogVisible(row) {
      console.log(row);
      this.editDialogVisible = true;
      this.editStationForm.id = row.id;
      (this.editStationForm.latitude = row.latitude),
        (this.editStationForm.longitude = row.longitude);
      (this.editStationForm.monitorTypeId = row.monitorTypeId),
        (this.editStationForm.sensorId = row.sensorId),
        (this.editStationForm.tailingPondId = row.tailingPondId),
        (this.editStationForm.unitId = row.unitId),
        (this.editStationForm.name = row.name);
      this.editStationForm.monitorCode = row.monitorCode;
    },
    //确认修改
    isEditdialogVisible() {
      this.$refs.editStationForm.validate((val) => {
        if (val) {
          const formData = new FormData();
          for (const [key, value] of Object.entries(this.editStationForm)) {
            formData.append(key, value);
          }
          axios({
            method: "post",
            url: "/monitorPoint/updateMonitorPoint",
            data: formData,
          }).then((res) => {
            console.log(res);
            if (res.data.resultCode == "200") {
              this.getStaionListPage();
              this.$message({
                type: "success",
                message: "修改成功",
                offset: 400,
                duration: 1000,
              });
              this.editDialogVisible = false;
            } else if (res.data.resultCode == "400") {
              this.$message({
                type: "error",
                message: "修改失败",
                offset: 400,
                duration: 1000,
              });
            }
          });
        } else {
          this.$message({
            type: "error",
            message: "请完成表单",
            offset: 400,
            duration: 1000,
          });
        }
      });
    },
    closeEditDialogVisible() {
      this.editDialogVisible = false;
    },
    //删除
    delStation(id) {
      this.$confirm("确定删除吗？")
        .then((_) => {
          axios({
            method: "get",
            url: "/monitorPoint/deleteMonitorPoint/" + `${id}`,
          }).then((res) => {
            if (res.data.resultCode == "200") {
              this.getStaionListPage();
              this.$message({
                type: "success",
                message: "删除成功",
                offset: 400,
                duration: 1000,
              });
            } else if (res.data.resultCode == "400") {
              this.$message({
                type: "error",
                message: "删除失败",
                offset: 400,
                duration: 1000,
              });
            }
          });
        })
        .catch((_) => {
          this.$message({
            type: "info",
            message: "已取消删除",
            offset: 400,
            duration: 1000,
          });
        });
    },
    setDialogWidth() {
      var val = document.body.clientWidth;
      const def = 750; // 默认宽度
      if (val < def) {
        this.dialogWidth = "100%";
      } else {
        this.dialogWidth = def + "px";
      }
    },
  },
  mounted() {
    window.onresize = () => {
      return (() => {
        this.setDialogWidth();
      })();
    };
  },
};
</script>

<style scoped>
.content {
  height: 87vh;
  overflow-y: auto;
}

.black-button {
  background-color: black;
  color: white;
}

.bg-purple {
  background: #d3dce6;
  height: 30px;
  font-size: 14px;
  line-height: 30px;
  padding-left: 10px;
}
.buttonfrom {
  margin-left: 200px;
  margin-top: 50px;
}
.el-button {
  font-size: 14px;
  margin-bottom: 8px;
}
.el-table {
  display: block;
  height: 67.2vh;
  overflow-y: auto;
  margin-bottom: 15px;
}
.el-table::before {
  z-index: inherit;
}
</style>
